package com.daoImpl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.LockMode;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.bean.CraftPartStandard;
import com.bean.ExtraCraftPartStandard;
import com.dao.CraftPartStandardDao;

/**
 * A data access object (DAO) providing persistence and search support for
 * CraftPartStandard entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.bean.CraftPartStandard
 * @author MyEclipse Persistence Tools
 */
public class CraftPartStandardDaoImpl extends HibernateDaoSupport implements CraftPartStandardDao {
	private static final Logger log = LoggerFactory
			.getLogger(CraftPartStandardDaoImpl.class);
	// property constants
	public static final String PART_ID = "partId";
	public static final String CRAFT_STD_TYPE_ID = "craftStdTypeId";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftPartStandardDao#save(com.bean.CraftPartStandard)
	 */
	@Override
	public void save(CraftPartStandard transientInstance) {
		log.debug("saving CraftPartStandard instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftPartStandardDao#delete(com.bean.CraftPartStandard)
	 */
	@Override
	public void delete(CraftPartStandard persistentInstance) {
		log.debug("deleting CraftPartStandard instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftPartStandardDao#findById(java.lang.Integer)
	 */
	@Override
	public CraftPartStandard findById(java.lang.Integer id) {
		log.debug("getting CraftPartStandard instance with id: " + id);
		try {
			CraftPartStandard instance = (CraftPartStandard) getHibernateTemplate()
					.get("com.bean.CraftPartStandard", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftPartStandardDao#findByExample(com.bean.CraftPartStandard)
	 */
	@Override
	public List findByExample(CraftPartStandard instance) {
		log.debug("finding CraftPartStandard instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftPartStandardDao#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding CraftPartStandard instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from CraftPartStandard as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftPartStandardDao#findByPartId(java.lang.Object)
	 */
	@Override
	public List findByPartId(Object partId) {
		return findByProperty(PART_ID, partId);
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftPartStandardDao#findByCraftStdTypeId(java.lang.Object)
	 */
	@Override
	public List findByCraftStdTypeId(Object craftStdTypeId) {
		return findByProperty(CRAFT_STD_TYPE_ID, craftStdTypeId);
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftPartStandardDao#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all CraftPartStandard instances");
		try {
			String queryString = "from CraftPartStandard";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftPartStandardDao#merge(com.bean.CraftPartStandard)
	 */
	@Override
	public CraftPartStandard merge(CraftPartStandard detachedInstance) {
		log.debug("merging CraftPartStandard instance");
		try {
			CraftPartStandard result = (CraftPartStandard) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftPartStandardDao#attachDirty(com.bean.CraftPartStandard)
	 */
	@Override
	public void attachDirty(CraftPartStandard instance) {
		log.debug("attaching dirty CraftPartStandard instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.daoImpl.CraftPartStandardDao#attachClean(com.bean.CraftPartStandard)
	 */
	@Override
	public void attachClean(CraftPartStandard instance) {
		log.debug("attaching clean CraftPartStandard instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static CraftPartStandardDao getFromApplicationContext(
			ApplicationContext ctx) {
		return (CraftPartStandardDao) ctx.getBean("craftPartStandardDao");
	}
	
	/**
	 * ��ȡ���й��ղ�����׼����
	 * @return
	 */
	public List<ExtraCraftPartStandard> getAllStandards(Map<String, String> options){
		Session session = getSession();  
		StringBuffer sqlsub = new StringBuffer("SELECT * from audit.craft_part_standard a " +
				"INNER JOIN audit.craft_standard_type b ON a.craft_std_type_id = b.craft_std_type_id where 0=0");
		SQLQuery query=null;
		List<ExtraCraftPartStandard> ecpStandards = new ArrayList<ExtraCraftPartStandard>();
		
		if(options.size()>0){
			if(options.size()>0){
				Iterator it = options.entrySet().iterator();
				while (it.hasNext()) {
				   Map.Entry entry = (Map.Entry) it.next();
				   Object value = entry.getValue();
				   sqlsub.append(" and ");
				}
			}
		}
		query=session.createSQLQuery(sqlsub.toString());
		
		List<Object[]> resultSet = query.list();
		
		for (Object[] r : resultSet) {
			ExtraCraftPartStandard ecpStandard = new ExtraCraftPartStandard();
			
			ecpStandard.setPartId((Integer)r[0]);
			ecpStandard.setPartId((Integer)r[1]);
			ecpStandard.setCraftStdTypeId((Integer)r[2]);
			ecpStandard.setCraftStdCode((String)r[4]);
			ecpStandard.setCraftStdName((String)r[5]);
			ecpStandard.setDescription((String)r[6]);
			
			ecpStandards.add(ecpStandard);
		}
		return ecpStandards;
	}
}